#!/usr/env/bin python
# -*- coding: utf-8 -*-

# @Time    : 2020-12-14 9:06 上午
# @Author  : yangdy
# @File    : 2020-12-14.49.字母异位词分组
# @Software: PyCharmCE
from collections import defaultdict

from tools.my_methods import *


class Solution:
    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
        if not strs:
            return []
        # data = []
        # for i in range(len(strs)):
        #     # e_arr = list(strs[i])
        #     # e_arr.sort()
        #     # data.append([strs[i], ''.join(e_arr)])
        #     data.append([strs[i], sorted(strs[i])])
        # data.sort(key=lambda x: x[1])
        # ans = [[data[0][0]]]
        # for i in range(1, len(data)):
        #     if data[i][1] == data[i-1][1]:
        #         ans[-1].append(data[i][0])
        #     else:
        #         ans.append([data[i][0]])
        # return ans
        mm = defaultdict(list)
        for s in strs:
            mm[''.join(sorted(s))] += [s]
        return list(mm.values())


questions = [
    (['eat', 'tea', 'tan', 'ate', 'nat', 'bat'],),
]

work(questions, lambda q: Solution().groupAnagrams(q[0]))
